import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonAddBooksToFolder.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
//import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import '../../controller/cache.dart';
import '../../controller/user.dart';
import '../../models/book/BookItem.dart';
import '../../models/book/BookSearchDto.dart';
import '../../themes/pageStyles.dart';
import '../../widgets/grids/bookGridWithCheckBox.dart';
//import '../../widgets/grids/singleBookDatagrid.dart';

class RecommendedBooksPage extends StatefulWidget {
  const RecommendedBooksPage(
      {required this.title, required this.bookSearchDto, Key? key})
      : super(key: key);
  final String title;
  final BookSearchDto bookSearchDto;
  _RecommendedBooksPage createState() => new _RecommendedBooksPage();
}

class _RecommendedBooksPage extends State<RecommendedBooksPage> {
  final BooksController _booksController = Get.find();
  final UserController _userController = Get.find();
  final PageStyles _pageStyles = PageStyles();
  //static const _pageSize = 15;
  late List<BookItem> newItems;
  final box = Hive.box('appconfig');
  bool setEvaluation = true;
  late String userName;
  late bool isStudent;
  late bool isIndividual;
  late bool isTeacher;
  late bool isAdmin;

  //late PagingController<int, BookItem> _pagingController;
  @override
  void initState() {
    _booksController.searchedBookItems.clear();

    //_pagingController = _booksController.getPagingController;
    if (_userController.profile.value.settings != null) {
      setEvaluation =
          _userController.profile.value.settings?.evaluation ?? true;
    }
    userName = box.get(CacheManagerKey['username'] ?? '');
    isStudent = box.get(CacheManagerKey['nanhairole'] ?? '') ==
        Constants.NanhaiRole_Student;
    isIndividual = box.get(CacheManagerKey['nanhairole'] ?? '') ==
        Constants.NanhaiRole_Individual;
    isTeacher = box.get(CacheManagerKey['nanhairole'] ?? '') ==
        Constants.NanhaiRole_Teacher;
    isAdmin = box.get(CacheManagerKey['nanhairole'] ?? '') ==
        Constants.NanhaiRole_SchoolAdmin;
    _booksController.dto = widget.bookSearchDto;
    _booksController.fetchSearchedBookItems(
        Constants.pageLimit, 0, widget.bookSearchDto, 'level');
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build common RecommendedBooksPage');
    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: CustomAppBar(title: widget.title),
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: _booksController.obx(
            (data) => BookGridWithCheckBox(
                  books: _booksController.searchedBookItems,
                  readingType: NanhaiBookReadingTypes.search,
                ),
            onEmpty: Center(child: Text('Nothing found!'.tr)),
            onLoading: Container()),
      ),
      floatingActionButton: FloatingActionButtonAddBookToFolder(
          readingType: NanhaiBookReadingTypes.search),
    );
  }
}
